home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / exts / blendeq.c.z / blendeq.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  7.5 KB  |  315 lines

  1. /*
  2. ** blendeq.c - Demonstrates the use of the blend_minmax, blend_subtract,
  3. **    and blend_logic_op extensions using glBlendEquationEXT.
  4. **
  5. **    Over a two-color backround, draw rectangles using twelve blend
  6. **    options.  The values are read back as UNSIGNED_BYTE and printed
  7. **    in hex over each value.  These values are useful for logic
  8. **    op comparisons when channels are 8 bits deep.
  9. */
  10.  
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include "tk.h"
  16.  
  17.  
  18. GLenum doubleBuffer, directRender;
  19. static int dithering = 0;
  20. static int doPrint = 1;
  21. static int deltaY;
  22. GLint windW, windH;
  23. GLuint bitmapBase;
  24.  
  25. static void Init(void)
  26. {
  27.     bitmapBase = glGenLists(256);
  28.     if (tkCreateBitmapFont(bitmapBase) == GL_FALSE) {
  29.     tkQuit();
  30.     }
  31.  
  32.     glDisable(GL_DITHER);
  33.     glShadeModel(GL_FLAT);
  34. }
  35.  
  36. static void Reshape(int width, int height)
  37. {
  38.  
  39.     windW = (GLint)width;
  40.     windH = (GLint)height;
  41.  
  42.     glViewport(0, 0, (GLint)width, (GLint)height);
  43.     deltaY = windH /16;
  44.  
  45.     glMatrixMode(GL_PROJECTION);
  46.     glLoadIdentity();
  47.     gluOrtho2D(0, windW, 0, windH);
  48.     glMatrixMode(GL_MODELVIEW);
  49. }
  50.  
  51. static GLenum Key(int key, GLenum mask)
  52. {
  53.  
  54.     switch (key) {
  55.       case TK_ESCAPE:
  56.     tkQuit();
  57.       case TK_d:
  58.     dithering = !dithering;
  59.     break;
  60.       default:
  61.     return GL_FALSE;
  62.     }
  63.     return GL_TRUE;
  64. }
  65.  
  66. static void PrintColorStrings()
  67. {
  68.     GLubyte ubbuf[3], ubcolor[3];
  69.     int i, xleft, xright;
  70.     char colorString[18];
  71.  
  72.     xleft = 5 + windW/4;
  73.     xright = 5 + windW/2;
  74.  
  75.     for (i = windH - deltaY + 4; i > 0; i-=deltaY) {
  76.         glReadPixels(xleft, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, ubbuf);
  77.         sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  78.                 ubbuf[0], ubbuf[1], ubbuf[2]);
  79.         glRasterPos2f(xleft, i);
  80.     tkDrawStr(bitmapBase, colorString);
  81.         glReadPixels(xright, i+10, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, ubbuf);
  82.         sprintf(colorString, "(0x%x, 0x%x, 0x%x)",
  83.                 ubbuf[0], ubbuf[1], ubbuf[2]);
  84.         glRasterPos2f(xright, i);
  85.     tkDrawStr(bitmapBase, colorString);
  86.     }
  87. }
  88.  
  89. static void Draw(void)
  90. {
  91.     float xscale, yscale;
  92.     GLfloat x, y;
  93.     int i, j;
  94.     GLfloat buf[3];
  95.     GLubyte ubbuf[3], ubcolor[3];
  96.     int stringOffset = 5, stringx = 8;
  97.     int x1, x2, xleft, xright;
  98.  
  99.  
  100.     (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  101.     glDisable(GL_BLEND);
  102.  
  103.     glClearColor(0.5, 0.6, 0.1, 1.0);
  104.     glClear(GL_COLOR_BUFFER_BIT);
  105.  
  106.     /* Draw background */
  107.     glColor3f(0.1, 0.1, 1.0);
  108.     glRectf(0.0, 0.0, windW/2, windH);
  109.  
  110.     /* Draw labels */
  111.     glColor3f(0.8, 0.8, 0.0);
  112.     i = windH - deltaY + stringOffset;
  113.     glRasterPos2f(stringx, i); i -= deltaY;
  114.     tkDrawStr(bitmapBase, "SOURCE");
  115.     glRasterPos2f(stringx, i); i -= deltaY;
  116.     tkDrawStr(bitmapBase, "DEST");
  117.     glRasterPos2f(stringx, i); i -= deltaY;
  118.     tkDrawStr(bitmapBase, "min");
  119.     glRasterPos2f(stringx, i); i -= deltaY;
  120.     tkDrawStr(bitmapBase, "max");
  121.     glRasterPos2f(stringx, i); i -= deltaY;
  122.     tkDrawStr(bitmapBase, "subtract");
  123.     glRasterPos2f(stringx, i); i -= deltaY;
  124.     tkDrawStr(bitmapBase, "reverse_subtract");
  125.     glRasterPos2f(stringx, i); i -= deltaY;
  126.     tkDrawStr(bitmapBase, "clear");
  127.     glRasterPos2f(stringx, i); i -= deltaY;
  128.     tkDrawStr(bitmapBase, "set");
  129.     glRasterPos2f(stringx, i); i -= deltaY;
  130.     tkDrawStr(bitmapBase, "copy");
  131.     glRasterPos2f(stringx, i); i -= deltaY;
  132.     tkDrawStr(bitmapBase, "noop");
  133.     glRasterPos2f(stringx, i); i -= deltaY;
  134.     tkDrawStr(bitmapBase, "and");
  135.     glRasterPos2f(stringx, i); i -= deltaY;
  136.     tkDrawStr(bitmapBase, "invert");
  137.     glRasterPos2f(stringx, i); i -= deltaY;
  138.     tkDrawStr(bitmapBase, "or");
  139.     glRasterPos2f(stringx, i); i -= deltaY;
  140.     tkDrawStr(bitmapBase, "xor");
  141.  
  142.  
  143.     i = windH - deltaY;
  144.     x1 = windW/4;
  145.     x2 = 3 * windW/4;
  146.     xleft = 5 + windW/4;
  147.     xright = 5 + windW/2;
  148.  
  149.     /* Draw foreground color for comparison */
  150.     glColor3f(0.9, 0.2, 0.8);
  151.     glRectf(x1, i, x2, i+deltaY);
  152.  
  153.     /* Leave one rectangle of background color */
  154.  
  155.     /* Begin test cases */
  156.     glEnable(GL_BLEND);
  157.     glBlendFunc(GL_ONE, GL_ONE);
  158.  
  159.     i -= 2*deltaY;
  160.     glBlendEquationEXT(GL_MIN_EXT);
  161.     glRectf(x1, i, x2, i+deltaY);
  162.  
  163.     i -= deltaY;
  164.     glBlendEquationEXT(GL_MAX_EXT);
  165.     glRectf(x1, i, x2, i+deltaY);
  166.  
  167.     i -= deltaY;
  168.     glBlendEquationEXT(GL_FUNC_SUBTRACT_EXT);
  169.     glRectf(x1, i, x2, i+deltaY);
  170.  
  171.     i -= deltaY;
  172.     glBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
  173.     glRectf(x1, i, x2, i+deltaY);
  174.  
  175.     glBlendFunc(GL_ONE, GL_ZERO);
  176.     i -= deltaY;
  177.     glBlendEquationEXT(GL_LOGIC_OP);
  178.     glLogicOp(GL_CLEAR);
  179.     glRectf(x1, i, x2, i+deltaY);
  180.  
  181.     i -= deltaY;
  182.     glBlendEquationEXT(GL_LOGIC_OP);
  183.     glLogicOp(GL_SET);
  184.     glRectf(x1, i, x2, i+deltaY);
  185.  
  186.     i -= deltaY;
  187.     glBlendEquationEXT(GL_LOGIC_OP);
  188.     glLogicOp(GL_COPY);
  189.     glRectf(x1, i, x2, i+deltaY);
  190.  
  191.     i -= deltaY;
  192.     glBlendEquationEXT(GL_LOGIC_OP);
  193.     glLogicOp(GL_NOOP);
  194.     glRectf(x1, i, x2, i+deltaY);
  195.  
  196.     i -= deltaY;
  197.     glBlendEquationEXT(GL_LOGIC_OP);
  198.     glLogicOp(GL_AND);
  199.     glRectf(x1, i, x2, i+deltaY);
  200.  
  201.     i -= deltaY;
  202.     glBlendEquationEXT(GL_LOGIC_OP);
  203.     glLogicOp(GL_INVERT);
  204.     glRectf(x1, i, x2, i+deltaY);
  205.  
  206.     i -= deltaY;
  207.     glBlendEquationEXT(GL_LOGIC_OP);
  208.     glLogicOp(GL_OR);
  209.     glRectf(x1, i, x2, i+deltaY);
  210.  
  211.     i -= deltaY;
  212.     glBlendEquationEXT(GL_LOGIC_OP);
  213.     glLogicOp(GL_XOR);
  214.     glRectf(x1, i, x2, i+deltaY);
  215.     glRectf(x1, i+10, x2, i+5);
  216.  
  217.   if (doPrint) {
  218.       glDisable(GL_BLEND);
  219.       glColor3f(1.0, 1.0, 1.0);
  220.       PrintColorStrings();
  221.   }
  222.   glFlush();
  223. }
  224.  
  225. static GLenum Args(int argc, char **argv)
  226. {
  227.     GLint i;
  228.  
  229.     doubleBuffer = GL_FALSE;
  230.     directRender = GL_TRUE;
  231.  
  232.     for (i = 1; i < argc; i++) {
  233.     if (strcmp(argv[i], "-sb") == 0) {
  234.         doubleBuffer = GL_FALSE;
  235.     } else if (strcmp(argv[i], "-db") == 0) {
  236.         doubleBuffer = GL_TRUE;
  237.     } else if (strcmp(argv[i], "-dr") == 0) {
  238.         directRender = GL_TRUE;
  239.     } else if (strcmp(argv[i], "-ir") == 0) {
  240.         directRender = GL_FALSE;
  241.     } else {
  242.         printf("%s (Bad option).\n", argv[i]);
  243.         return GL_FALSE;
  244.     }
  245.     }
  246.     return GL_TRUE;
  247. }
  248.  
  249.  
  250. static GLboolean queryExtension(char *extName)
  251. {
  252.     /*
  253.     ** Search for extName in the extensions string.  Use of strstr()
  254.     ** is not sufficient because extension names can be prefixes of
  255.     ** other extension names.  Could use strtok() but the constant
  256.     ** string returned by glGetString can be in read-only memory.
  257.     */
  258.     char *p = (char *) glGetString(GL_EXTENSIONS);
  259.     char *end = p + strlen(p);
  260.     while (p < end) {
  261.     int n = strcspn(p, " ");
  262.     if ((strlen(extName) == n) && (strncmp(extName, p, n) == 0)) {
  263.         return GL_TRUE;
  264.     }
  265.     p += (n + 1);
  266.     }
  267.     return GL_FALSE;
  268. }
  269.  
  270.  
  271. void main(int argc, char **argv)
  272. {
  273.     GLenum type;
  274.     const GLubyte *s;
  275.  
  276.     if (Args(argc, argv) == GL_FALSE) {
  277.     tkQuit();
  278.     }
  279.  
  280.     tkInitPosition(0, 0, 800, 400);
  281.  
  282.     type = TK_RGB;
  283.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  284.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  285.     tkInitDisplayMode(type);
  286.  
  287.     if (tkInitWindow("Blend Equation") == GL_FALSE) {
  288.     tkQuit();
  289.     }
  290.  
  291.     /* Make sure blend_logic_op extension is there. */
  292.     if (!queryExtension("GL_EXT_blend_logic_op")) {
  293.        printf("Blend_logic_op extension is not present.\n");
  294.        tkQuit();
  295.     }
  296.  
  297.     if (!queryExtension("GL_EXT_blend_minmax")) {
  298.        printf("Blend_minmax extension is not present.\n");
  299.        tkQuit();
  300.     }
  301.  
  302.     if (!queryExtension("GL_EXT_blend_subtract")) {
  303.     printf("Blend_subtract extension is not present.\n");
  304.        tkQuit();
  305.     }
  306.  
  307.     Init();
  308.  
  309.     tkExposeFunc(Reshape);
  310.     tkReshapeFunc(Reshape);
  311.     tkKeyDownFunc(Key);
  312.     tkDisplayFunc(Draw);
  313.     tkExec();
  314. }
  315.